# -*- coding: UTF-8 -*-
"""
Use class FittableLabel or in yours
FittableLable object inherits Lable and
FittableButton object inherits Button adding folowing attributes:
length: (positive int) max length of content (default 0)
    if is 0 calculated max(len(self.text),1))
fit_height: (bool) whether widget should fit font_size to its height
            (default True)
coef: (positive float) (less than 1) calculated font-size is multiplied
      on this attribute

color_name: (str) color name (default 'default')

look in add_words.kv file for example
"""

import datetime
import os.path
from kivy.resources import resource_add_path
from kivy.logger import Logger
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.togglebutton import ToggleButton
from kivy.lang import Builder

from kivy.properties import BoundedNumericProperty, BooleanProperty
from kivy.properties import StringProperty
from constants import COLOR, PATH


resource_add_path(os.path.join(PATH, 'assets'))
Builder.load_string('''
<FittableLabel>:
    font_name: 'Roboto-Regular.ttf'
<FittableTextInput>:
    background_active: "atlas://assets/defaulttheme/textinput_active"
    background_normal: "atlas://assets/defaulttheme/textinput"
    foreground_color: 1,1,1,1
    font_size: min(self.size[1]*0.6,self.size[0]/max(self.length,1)*1.4)
    font_name: 'Roboto-Regular.ttf'
    length: 3
''')


def get_time():
    return unicode(datetime.datetime.now().strftime('%m.%d_%H.%M.%S'))


class FittableLabel(Label):
    length = BoundedNumericProperty(0, min=0)
    coef = BoundedNumericProperty(1, min=0, max=1)
    fit_height = BooleanProperty(True)
    color_name = StringProperty('default')

    def __init__(self, **kwargs):
        super(FittableLabel, self).__init__(**kwargs)
        self.bind(size=self.on_text, length=self.on_text)

    def on_text(self, instance, value):
        if self.length == 0:
            length = max(max(map(len, self.text.split('\n'))), 1)
        else:
            length = self.length
        self.font_size = self.coef * \
            min(self.size[0]/length*1.4,
                self.size[1]*0.6 if self.fit_height else 'inf')

    def on_color_name(self, instance, value):
        self.color = COLOR[value]


class FittableButton(Button, FittableLabel):
    def __init__(self, **kwargs):
        super(FittableButton, self).__init__(**kwargs)
        self.bind(on_release=self.to_log)

    def to_log(self, *args):
        Logger.debug((u'LOG ({}): \'{}\' button pressed'.
                      format(get_time(), self.text)).encode('utf-8'))


class FittableToggleButton(ToggleButton, FittableLabel):
    def __init__(self, **kwargs):
        super(FittableToggleButton, self).__init__(**kwargs)
        self.bind(on_release=self.to_log)

    def to_log(self, *args):
        Logger.debug((u'LOG ({}): \'{}\' toggle button pressed'.
                      format(get_time(), self.text)).encode('utf-8'))


class FittableTextInput(TextInput):
    def __init__(self, **kwargs):
        super(FittableTextInput, self).__init__(**kwargs)

    def on_text(self, instance, value):
        pass
